package com.hrrt.home.admin.controller.manage;

import com.hrrt.common.base.BaseController;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.*;
import com.hrrt.home.rpc.api.HomeBaseUnitService;
import com.hrrt.home.rpc.api.HomeGatewayService;
import com.hrrt.home.rpc.api.HomeRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 基础设备controller
 * Created by JiangyanPeng on 2017/09/01.
 */
@Controller
@RequestMapping("/manage/homeroom")
@Api(value = "基础设备", description = "基础设备")

public class HomeRoomController extends BaseController {
    @Autowired
    HomeRoomService homeRoomService;
    @Autowired
    HomeGatewayService homeGatewayService;
    @Autowired
    HomeBaseUnitService homeBaseUnitService ;

    private static Logger _log = LoggerFactory.getLogger(HomeBaseUnitController.class);
    @ApiOperation(value = "用户房间首页")
    @RequiresPermissions("home:homeroom:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/homeroom/index.jsp";
    }
    @ApiOperation(value = "用户房间列表")
    @RequestMapping(value="list",method = RequestMethod.GET)
    @RequiresPermissions("home:homeroom:read")
    @ResponseBody
    public Object list (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order){
        HomeRoomExample homeRoomExample = new HomeRoomExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeRoomExample.setOrderByClause(sort + " " + order);
        }
        List<HomeRoom> rows = homeRoomService.selectByExampleForOffsetPage(homeRoomExample,offset,limit);
        for(HomeRoom list : rows){
            String name = homeBaseUnitService.selectByPrimaryKey(list.getRoomBasicId()).getText();
            list.setBasename(name);
        }
        long total = homeRoomService.countByExample(homeRoomExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增用户房间")
    @RequiresPermissions("home:homeroom:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        HomeBaseUnitExample homeBaseUnitExample = new HomeBaseUnitExample();
        List<HomeBaseUnit> homeBaseUnits = homeBaseUnitService.selectByExample(homeBaseUnitExample);
        modelMap.put("homeBaseUnits",homeBaseUnits);

        HomeGatewayExample homeGatewayExample = new HomeGatewayExample();
        List<HomeGateway> homeGateways = homeGatewayService.selectByExample(homeGatewayExample);
        modelMap.put("homeGateways",homeGateways);
        return "/manage/homeroom/create.jsp";
    }

    @ApiOperation(value = "新增用户房间")
    @RequiresPermissions("home:homeroom:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeRoom homeRoom) {
        Date date = new Date();
        homeRoom.setCreateTime(date);

        /*更改网关表状态和绑定房产*/
        HomeGateway homeGateway = new HomeGateway();
        Integer gatewayId = homeRoom.getGatewayId();
//        String suiteId = homeRoom.getSuiteId();

        homeGateway.setId(gatewayId);
//        homeGateway.setSuiteId(suiteId);
        homeGateway.setStatus("1");

        homeGatewayService.updateByPrimaryKeySelective(homeGateway);
        int count = homeRoomService.insertSelective(homeRoom);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改用户房间")
    @RequiresPermissions("home:homeroom:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {

        HomeBaseUnitExample homeBaseUnitExample = new HomeBaseUnitExample();
        List<HomeBaseUnit> homeBaseUnits = homeBaseUnitService.selectByExample(homeBaseUnitExample);
        modelMap.put("homeBaseUnits",homeBaseUnits);

        HomeGatewayExample homeGatewayExample = new HomeGatewayExample();
        List<HomeGateway> homeGateways = homeGatewayService.selectByExample(homeGatewayExample);
        modelMap.put("homeGateways",homeGateways);

        HomeRoom homeRoom = homeRoomService.selectByPrimaryKey(id);
        modelMap.put("homeRoom", homeRoom);
        return "/manage/homeroom/update.jsp";
    }

    @ApiOperation(value = "修改用户房间")
    @RequiresPermissions("home:homeroom:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeRoom homeRoom) {
        homeRoom.setId(id);
        int count = homeRoomService.updateByPrimaryKeySelective(homeRoom);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除基础设备")
    @RequiresPermissions("home:homeroom:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeRoomService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

}
